package com.material.demo.lyric;

import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by pc on 2016/12/23.
 */

public class ZrceParser extends BaseParser {

    private List<LyricInfo> mLyrics;
    private int offset;
    private String ti;
    private String ar;
    private int total;

    private static final String REGEX = "<[0-9]{1,},[0-9]{1,},[0-9]{1,}>";


    @Override
    protected void onReadFileCallBack(String content) {

        String[] lyrics = content.split("\n");
        if (lyrics != null && lyrics.length > 0) {

            for (String info : lyrics) {
                if (TextUtils.isEmpty(info)) {
                    continue;
                }


                if (info.startsWith("[") && info.contains("]") && !info.contains("<") && !info.contains(">")) {
                    //头部信息
                    String temp = info
                            .replace("[", "{")
                            .replace("]", "}");
                    try {
                        JSONObject json = new JSONObject(temp);
                        offset = json.optInt("offset");
                        total = json.optInt("total");
                        ar = json.optString("ar");
                        ti = json.optString("ti");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    //歌词行
                    logMsg("歌词 : " + info);
                    String temp = info;
                    StringBuffer buffer = new StringBuffer();

                    LyricInfo lyricInfo = new LyricInfo();
                    int start = temp.indexOf("[");
                    int end = temp.indexOf("]");

                    if(start!=-1 && end!=-1 && end>+start+1){
                        String[] times = temp.substring(start+1,end).split(",");
                        lyricInfo.startTime = Integer.parseInt(times[0]);
                        lyricInfo.endTime = lyricInfo.startTime + Integer.parseInt(times[1]);
                    }

                    int upGroupEndIndex = 0;
                    String upGroup = null;
                    Pattern pattern = Pattern.compile(REGEX);
                    Matcher matcher = pattern.matcher(temp);
                    CharSequence sequence = null;

                    while (matcher.find()){
                        if(upGroupEndIndex > 0){
                            int len = matcher.start() - upGroupEndIndex;
                            if(len>0) {
                                sequence = temp.subSequence(upGroupEndIndex,matcher.start());
                                buffer.append(sequence);

                                if(lyricInfo.words == null){
                                    lyricInfo.words = new ArrayList<>();
                                }
                                lyricInfo.words.add(createWord(sequence,upGroup,lyricInfo));
                            }
                        }

                        upGroup = matcher.group();
                        upGroupEndIndex = matcher.end();
                    }

                    //截取最后一个字
                    if(temp.length()>upGroupEndIndex) {
                        sequence = temp.subSequence(upGroupEndIndex, temp.length());
                        buffer.append(sequence);
                        if(lyricInfo.words!=null) {
                            lyricInfo.words.add(createWord(sequence, upGroup, lyricInfo));
                        }
                    }

                    lyricInfo.content = buffer.toString();

                    if(mLyrics == null){
                        mLyrics = new ArrayList<>();
                    }
                    mLyrics.add(lyricInfo);
                }
            }
        }
    }

    private LyricWord createWord(CharSequence sequence,String upGroup,LyricInfo lyricInfo){
        LyricWord word = new LyricWord();
        word.word = sequence;
        word.lyricStartTime = lyricInfo.startTime;
        word.lyricEndTime = lyricInfo.endTime;

        if(!TextUtils.isEmpty(upGroup)){
            upGroup = upGroup.replace("<","").replace(">","");
            String[] textTime = upGroup.split(",");
            word.startTime = word.lyricStartTime + Integer.parseInt(textTime[0]);
            word.endTime = word.lyricStartTime + Integer.parseInt(textTime[1]);
        }
        return word;
    }

    @Override
    public List getLyrics() {
        return mLyrics;
    }

    @Override
    public int getOffset() {
        return offset;
    }

    @Override
    protected String getTag() {
        return "Chat";
    }

    @Override
    public void measureTextSize(Paint paint) {
        if(paint!=null && mLyrics!=null && mLyrics.size()>0){
            int size = mLyrics.size();
            for(int i=0;i<size;i++){
                LyricInfo item = mLyrics.get(i);
                Rect rect = new Rect();
                paint.getTextBounds(item.content,0,item.content.length(),rect);
                item.contentWidth = rect.width();
                item.contentHeight = rect.height();
                if(item.words!=null && item.words.size()>0){
                    int len = item.words.size();
                    for(int j=0;j<len;j++){
                        LyricWord word = item.words.get(j);
                        word.wordWidth = paint.measureText((String) word.word);
                    }
                }
            }
        }
    }
}
